import storage from '@/utils/storage'

export const listMixin = {
  data() {
    return {
      //所有列表数据
      list: [],
      //总行数
      total: 0,
      //查询参数
      queryParams: {
        //第几页
        pageNum: 1,
        //一页几行
        pageSize: 10,
        //条件
        params: {

        },
      },
      loadStatus: 'loadmore',
      options: [
      	{text: '删除', style: { background: '#dd524d'}}
      ],
	  arrow: false,
    };
  },
  // 监听 - 页面触底部
  onReachBottom(options) {
  	this.loadMore();
  },
  onShow: function() {
  	let isRefresh = storage.get("isRefresh");
  	if (isRefresh == 1){
		console.log("isRefresh")
  		storage.remove("isRefresh");
  		this.search();
  	}	    
  },
  methods: {
	loadMore() {
		if (this.loadStatus == "loadmore") {
			this.loadStatus = "loading";
			setTimeout(() => {
				this.queryParams.pageNum += 1;
				this.fetchData();
			}, 100);
		}		
	},  
	//获取数据
	fetchData() {
		//调用list接口
		this.callList(this.queryParams).then(res => {
			const { rows } = res;
			this.list = this.list.concat(rows);
			this.total = res.total;
			this.loadStatus = "loadmore";
			//判断是否还有数据，是否能加载更多
			if (this.queryParams.pageNum > 1) {
				// 翻页
				if (rows.length == 0) {
					// 无数据时
					this.loadStatus = "nomore";
				} else {
					// 有数据时
					if (rows.length < this.queryParams.pageSize) {
						this.loadStatus = "nomore";
					}
				}
			} else if (this.queryParams.pageNum == 1) {
				if (rows.length < this.queryParams.pageSize) {
					this.loadStatus = "nomore";
				}
			}
		}).catch(err => {
			if (this.queryParams.pageNum > 1) { this.queryParams.pageNum--; }
		}); 	       
	},
	search(value) {
		this.list = [];
		this.queryParams.pageNum = 1;
		this.fetchData();
	},
	//点击删除
	handleDelete(row, index) {
		this.callDelete(row.id, index).then(res => {
			this.$u.toast("删除成功！");
			this.list.splice(index, 1);
		});
	},
	optionsClick(rowIndex, btnIndex) {
		if(btnIndex == 0) {
			let _this = this;
			uni.showModal({
				title: '提示',
				content: '确认要删除该数据吗？',
				showCancel: true,
				success: function (res2) {
					if (res2.confirm) {
						let row = _this.list[rowIndex];
						_this.handleDelete(row, rowIndex);
					}
				}
			});
		}
	}	
  }
}
  